package com.cloud.wms.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
import android.view.KeyEvent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.cloud.core.multistate.NavIconType;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.tools.BarAdapter;
import com.cloud.wms.ui.main.contract.MainContract;
import com.cloud.wms.ui.main.entity.TaskMenuEntity;
import com.cloud.wms.ui.main.fragment.MineTaskFragment;
import com.cloud.wms.ui.main.fragment.ModelFragment;
import com.cloud.wms.ui.main.fragment.WaitTaskFragment;
import com.cloud.wms.ui.main.presnter.MainPresenter;
import com.dylanc.loadinghelper.ViewType;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseBarActivity<MainPresenter> implements MainContract.View {
    private static final int[] MENU_ID = {R.id.page_model, R.id.page_wait_task, R.id.page_mine_task};

    @BindView(R.id.vp_content)
    ViewPager vpContent;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private ModelFragment modelFragment;
    private WaitTaskFragment waitTaskFragment;
    private MineTaskFragment mineTaskFragment;

    @Override
    protected String barTitle() {
        return "首页";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    protected void initToolBar() {
        loadingHelper.register(ViewType.TITLE, new BarAdapter(barTitle(), NavIconType.NONE, true, v -> switchBaseInfo()));
        loadingHelper.setDecorHeader(ViewType.TITLE);
    }

    @Override
    protected void initEvent() {
        ArrayList<TaskMenuEntity> taskMenu = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("TaskMenu", taskMenu);

        fragments.add(modelFragment = new ModelFragment());
        fragments.add(waitTaskFragment = new WaitTaskFragment());
        waitTaskFragment.setArguments(bundle);
        fragments.add(mineTaskFragment = new MineTaskFragment());
        mineTaskFragment.setArguments(bundle);

        FragmentAdapter fragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), fragments);
        vpContent.setAdapter(fragmentAdapter);

        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                navigation.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        navigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.page_model:
                    if (!item.isChecked())
                        scroll(0);
                    break;
                case R.id.page_wait_task:
                    if (item.isChecked()) mPresenter.loadWaitTask(waitTaskFragment.getTaskCode());
                    else scroll(1);
                    break;
                case R.id.page_mine_task:
                    if (item.isChecked()) mPresenter.loadMineTask(mineTaskFragment.getTaskCode());
                    else scroll(2);
                    break;
            }
            return false;
        });
    }

    private void scroll(int index) {
        boolean smoothScroll = (vpContent.getCurrentItem() + 1 == index) || (vpContent.getCurrentItem() + 1 == index);
        vpContent.setCurrentItem(index, smoothScroll);
    }

    @Override
    public void onSuccess(Message message) {

    }

    @Override
    public void switchBaseInfo() {
        super.switchBaseInfo();
        BaseInfoActivity.start(this, false);
    }

    /**
     * 两次点击退出程序 间隔时间2秒
     */
    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                info("再按一次退出程序");
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public static void start(Context context, ArrayList<TaskMenuEntity> taskMenuList) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(IntentKey.LIST_KEY, taskMenuList);
        context.startActivity(intent);
    }


    class FragmentAdapter extends FragmentStatePagerAdapter {

        private final ArrayList<Fragment> list;

        public FragmentAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
